home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.5 / curses / textpad.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-29  |  6KB  |  204 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Simple textbox editing widget with Emacs-like keybindings.'''
  5. import curses
  6. import ascii
  7.  
  8. def rectangle(win, uly, ulx, lry, lrx):
  9.     '''Draw a rectangle with corners at the provided upper-left
  10.     and lower-right coordinates.
  11.     '''
  12.     win.vline(uly + 1, ulx, curses.ACS_VLINE, lry - uly - 1)
  13.     win.hline(uly, ulx + 1, curses.ACS_HLINE, lrx - ulx - 1)
  14.     win.hline(lry, ulx + 1, curses.ACS_HLINE, lrx - ulx - 1)
  15.     win.vline(uly + 1, lrx, curses.ACS_VLINE, lry - uly - 1)
  16.     win.addch(uly, ulx, curses.ACS_ULCORNER)
  17.     win.addch(uly, lrx, curses.ACS_URCORNER)
  18.     win.addch(lry, lrx, curses.ACS_LRCORNER)
  19.     win.addch(lry, ulx, curses.ACS_LLCORNER)
  20.  
  21.  
  22. class Textbox:
  23.     '''Editing widget using the interior of a window object.
  24.      Supports the following Emacs-like key bindings:
  25.  
  26.     Ctrl-A      Go to left edge of window.
  27.     Ctrl-B      Cursor left, wrapping to previous line if appropriate.
  28.     Ctrl-D      Delete character under cursor.
  29.     Ctrl-E      Go to right edge (stripspaces off) or end of line (stripspaces on).
  30.     Ctrl-F      Cursor right, wrapping to next line when appropriate.
  31.     Ctrl-G      Terminate, returning the window contents.
  32.     Ctrl-H      Delete character backward.
  33.     Ctrl-J      Terminate if the window is 1 line, otherwise insert newline.
  34.     Ctrl-K      If line is blank, delete it, otherwise clear to end of line.
  35.     Ctrl-L      Refresh screen.
  36.     Ctrl-N      Cursor down; move down one line.
  37.     Ctrl-O      Insert a blank line at cursor location.
  38.     Ctrl-P      Cursor up; move up one line.
  39.  
  40.     Move operations do nothing if the cursor is at an edge where the movement
  41.     is not possible.  The following synonyms are supported where possible:
  42.  
  43.     KEY_LEFT = Ctrl-B, KEY_RIGHT = Ctrl-F, KEY_UP = Ctrl-P, KEY_DOWN = Ctrl-N
  44.     KEY_BACKSPACE = Ctrl-h
  45.     '''
  46.     
  47.     def __init__(self, win):
  48.         self.win = win
  49.         (self.maxy, self.maxx) = win.getmaxyx()
  50.         self.maxy = self.maxy - 1
  51.         self.maxx = self.maxx - 1
  52.         self.stripspaces = 1
  53.         self.lastcmd = None
  54.         win.keypad(1)
  55.  
  56.     
  57.     def _end_of_line(self, y):
  58.         '''Go to the location of the first blank on the given line.'''
  59.         last = self.maxx
  60.         while ascii.ascii(self.win.inch(y, last)) != ascii.SP:
  61.             last = min(self.maxx, last + 1)
  62.             break
  63.         if last == 0:
  64.             break
  65.         
  66.         last = last - 1
  67.         continue
  68.         return last
  69.  
  70.     
  71.     def do_command(self, ch):
  72.         '''Process a single editing command.'''
  73.         (y, x) = self.win.getyx()
  74.         self.lastcmd = ch
  75.         if ascii.isprint(ch):
  76.             if y < self.maxy or x < self.maxx:
  77.                 
  78.                 try:
  79.                     self.win.addch(ch)
  80.                 except curses.error:
  81.                     pass
  82.                 except:
  83.                     None<EXCEPTION MATCH>curses.error
  84.                 
  85.  
  86.             None<EXCEPTION MATCH>curses.error
  87.         elif ch == ascii.SOH:
  88.             self.win.move(y, 0)
  89.         elif ch in (ascii.STX, curses.KEY_LEFT, ascii.BS, curses.KEY_BACKSPACE):
  90.             if x > 0:
  91.                 self.win.move(y, x - 1)
  92.             elif y == 0:
  93.                 pass
  94.             elif self.stripspaces:
  95.                 self.win.move(y - 1, self._end_of_line(y - 1))
  96.             else:
  97.                 self.win.move(y - 1, self.maxx)
  98.             if ch in (ascii.BS, curses.KEY_BACKSPACE):
  99.                 self.win.delch()
  100.             
  101.         elif ch == ascii.EOT:
  102.             self.win.delch()
  103.         elif ch == ascii.ENQ:
  104.             if self.stripspaces:
  105.                 self.win.move(y, self._end_of_line(y))
  106.             else:
  107.                 self.win.move(y, self.maxx)
  108.         elif ch in (ascii.ACK, curses.KEY_RIGHT):
  109.             if x < self.maxx:
  110.                 self.win.move(y, x + 1)
  111.             elif y == self.maxy:
  112.                 pass
  113.             else:
  114.                 self.win.move(y + 1, 0)
  115.         elif ch == ascii.BEL:
  116.             return 0
  117.         elif ch == ascii.NL:
  118.             if self.maxy == 0:
  119.                 return 0
  120.             elif y < self.maxy:
  121.                 self.win.move(y + 1, 0)
  122.             
  123.         elif ch == ascii.VT:
  124.             if x == 0 and self._end_of_line(y) == 0:
  125.                 self.win.deleteln()
  126.             else:
  127.                 self.win.move(y, x)
  128.                 self.win.clrtoeol()
  129.         elif ch == ascii.FF:
  130.             self.win.refresh()
  131.         elif ch in (ascii.SO, curses.KEY_DOWN):
  132.             if y < self.maxy:
  133.                 self.win.move(y + 1, x)
  134.                 if x > self._end_of_line(y + 1):
  135.                     self.win.move(y + 1, self._end_of_line(y + 1))
  136.                 
  137.             
  138.         elif ch == ascii.SI:
  139.             self.win.insertln()
  140.         elif ch in (ascii.DLE, curses.KEY_UP):
  141.             if y > 0:
  142.                 self.win.move(y - 1, x)
  143.                 if x > self._end_of_line(y - 1):
  144.                     self.win.move(y - 1, self._end_of_line(y - 1))
  145.                 
  146.             
  147.         
  148.         return 1
  149.  
  150.     
  151.     def gather(self):
  152.         '''Collect and return the contents of the window.'''
  153.         result = ''
  154.         for y in range(self.maxy + 1):
  155.             self.win.move(y, 0)
  156.             stop = self._end_of_line(y)
  157.             if stop == 0 and self.stripspaces:
  158.                 continue
  159.             
  160.             for x in range(self.maxx + 1):
  161.                 if self.stripspaces and x > stop:
  162.                     break
  163.                 
  164.                 result = result + chr(ascii.ascii(self.win.inch(y, x)))
  165.             
  166.             if self.maxy > 0:
  167.                 result = result + '\n'
  168.                 continue
  169.         
  170.         return result
  171.  
  172.     
  173.     def edit(self, validate = None):
  174.         '''Edit in the widget window and collect the results.'''
  175.         while None:
  176.             ch = self.win.getch()
  177.             if validate:
  178.                 ch = validate(ch)
  179.             
  180.             if not ch:
  181.                 continue
  182.             
  183.             if not self.do_command(ch):
  184.                 break
  185.             
  186.             continue
  187.             return self.gather()
  188.  
  189.  
  190. if __name__ == '__main__':
  191.     
  192.     def test_editbox(stdscr):
  193.         (ncols, nlines) = (9, 4)
  194.         (uly, ulx) = (15, 20)
  195.         stdscr.addstr(uly - 2, ulx, 'Use Ctrl-G to end editing.')
  196.         win = curses.newwin(nlines, ncols, uly, ulx)
  197.         rectangle(stdscr, uly - 1, ulx - 1, uly + nlines, ulx + ncols)
  198.         stdscr.refresh()
  199.         return Textbox(win).edit()
  200.  
  201.     str = curses.wrapper(test_editbox)
  202.     print 'Contents of text box:', repr(str)
  203.  
  204.